package org.zhouhe.tag.report;

import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.zhouhe.modules.report.service.ReportService;
import org.zhouhe.vo.Report;
import org.zhouhe.vo.ReportType;

public class ReportSimple extends TagSupport {

	private static final long serialVersionUID = 3886850494335003326L;
	private ServletContext servletContext;
	private WebApplicationContext wac;
	private ReportService reportService;

	private Integer typeId;
	private Integer industryId;
	private Integer resultSize = 10;
	private String className;

	public int doStartTag() {
		try {
			StringBuilder html = new StringBuilder();
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			servletContext = request.getSession().getServletContext();
			wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			reportService = (ReportService) wac.getBean("reportService");
			ReportType reportType = reportService.getReportType(typeId);
			List<Report> reportList = reportService.getReportByType(typeId, industryId, getResultSize(), true);
			html.append("<div class='"+ className +"'>");
			html.append("  <ul class='blockTag-title-ul'>");
			html.append("    <li class='blockTag-title-text'>"+ reportType.getName() +"</li>");
			html.append("    <li style='float:right'><a target='_blank' href='reportList4txix.action?reportType.reportTypeId="+ typeId +"'>更多&gt;&gt;</a></li>");
			html.append("  </ul>");
			html.append("");
			html.append("  <div class='blockTag-content-div'>");
			html.append("      <ul class='blockSimple-content-list-ul'>");
			if (reportList.size() > 0) {
				for (Report rpt : reportList) {
					html.append("      <li>• <a target='_blank' href='report.action?report.reportId="+ rpt.getReportId() +"'>" + rpt.getTitle() + "</a></li>");
				}
			}else{
				html.append("      <li style='border:0px;text-align:center'>暂时没有数据！</li>");
			}
			html.append("      </ul>");
			html.append("  </div>");
			html.append("  <div style='clear:both;'></div>");
			html.append("</div>");
			html.append("");
			html.append("");
			html.append("");
			JspWriter out = pageContext.getOut();
			out.print(html.toString());
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SKIP_BODY;
	}

	public void setTypeId(Integer typeId) {
		this.typeId = typeId;
	}

	public Integer getTypeId() {
		return typeId;
	}

	public void setResultSize(Integer resultSize) {
		this.resultSize = resultSize;
	}

	public Integer getResultSize() {
		return resultSize;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public String getClassName() {
		return className;
	}

	public void setIndustryId(Integer industryId) {
		this.industryId = industryId;
	}

	public Integer getIndustryId() {
		return industryId;
	}

}
